package com.zkingsoft.service.impl;

import com.zkingsoft.service.BizAuthService;
import com.matrix.core.constance.SystemErrorCode;
import com.zkingsoft.bean.BizAuth;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.dao.BizAuthDao;

import org.springframework.stereotype.Service;

import java.util.Map;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import com.matrix.core.tools.WebUtil;
import com.matrix.core.tools.UUIDUtil;

/**
 * @description service接口实现类(认证信息表)
 * @author 肖崇高
 * @date 2018-05-23 20:39
 */
@Service
public class  BizAuthServiceImpl implements  BizAuthService{

	
	@Autowired
	private BizAuthDao bizAuthDao;
	
	
	@Override
	public int add(BizAuth bizAuth){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizAuth.setCreateBy(loginUser.getSuName());
		bizAuth.setUpdateBy(loginUser.getSuName());
		bizAuth.setId(UUIDUtil.getRandomID());
		return bizAuthDao.insert(bizAuth);
		
	}
	
	@Override
	public int batchAdd(List<BizAuth>  bizAuthList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizAuthList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizAuthList.size() ? bizAuthList.size() : end;
			List<BizAuth> insertList = bizAuthList.subList(begin, end);
			num += bizAuthDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizAuth oldBizAuth
	,BizAuth newBizAuth){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizAuth, newBizAuth)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizAuth, newBizAuth);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizAuth);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("id", oldBizAuth.getId());
			bizAuthDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizAuth bizAuth){
	
		return bizAuthDao.updateByModel(bizAuth);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizAuthDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String id){
	
		return bizAuthDao.deleteById(id);
	
	}
	
	@Override
	public int removeByModel(BizAuth bizAuth){
	
		return bizAuthDao.deleteByModel(bizAuth);
	
	}
	
	
	@Override
	public List<BizAuth> findInPage(BizAuth bizAuth,  PaginationVO pageVo){
	
		return bizAuthDao.selectInPage(bizAuth , pageVo);
	
	}
	
	@Override
	public List<BizAuth> findByModel(BizAuth bizAuth){
	
		return bizAuthDao.selectByModel(bizAuth);
	
	}
	
	@Override
	public int  findTotal(BizAuth bizAuth){
	
		return bizAuthDao.selectTotalRecord(bizAuth);
	
	}
	
	@Override
	public BizAuth  findById(String id){
	
		return bizAuthDao.selectById(id);
	
	}

}